<?php

namespace app\controller;

use app\BaseController;

use think\facade\Db;

class Index extends BaseController
{

    //登录
    public function login()
    {
        $user = input('name');
        $password = input('password');

        $where[] = ['name', '=', $user];
        $where[] = ['password', '=', $password];
        $re = Db::table('qc_user')->where($where)->find();

        if ($re) {
            return reJson([
                'token' => md5($user)
            ], 200, '成功');
        } else {
            return reJson([], 500, '失败');
        }
    }

    //新增修改
    public function save()
    {
        $data = input('');
        $re = Db::table('qc_key')->save($data);
        if ($re) {
            return reJson($re, 200, '成功');
        } else {
            return reJson([], 500, '失败');
        }
    }

    //删除
    public function delete()
    {
        $data = input('');
        $re = Db::table('qc_key')->delete($data);
        if ($re) {
            return reJson($re, 200, '成功');
        } else {
            return reJson([], 500, '失败');
        }
    }

    //关键字自增
    public function inc()
    {
        $data = input('');

        $re = Db::table('qc_key')
            ->where('id', $data['id'])
            ->inc('count')
            ->update();

        if ($re) {
            return reJson($re, 200, '成功');
        } else {
            return reJson([], 500, '失败');
        }
    }

    //查询关键字回复
    public function getKey()
    {
        $data = input('');
        $re = Db::table('qc_key')->where($data)->find();
        if ($re) {
            Db::table('qc_key')
            ->where('id', $re['id'])
            ->inc('count')
            ->update();
            
            $re['img'] = !empty($re['img']) ? explode(",",$re['img']) : '';
            $re['video'] = !empty($re['video']) ? explode(",",$re['video']) : '';
            $re['type'] = 'robot';
            $re['name'] = '智能客服';
            return reJson($re, 200, '成功');
        } else {
            $re = [];
            $re['type'] = 'robot';
            $re['name'] = '智能客服';
            $re['content'] = '未找到相关内容~试试点击热门关键字进行咨询。';
            return reJson($re, 500, '失败');
        }
    }

    


    //关键字列表
    public function list()
    {
        $data = input('');
        
        $orderKey = isset($data['orderKey']) ? $data['orderKey'] : 'id';
        $orderSort = isset($data['orderSort']) ? $data['orderSort'] : 'desc';
        
        $re = Db::table('qc_key')->order($orderKey, $orderSort)->paginate([
            'list_rows' => 10,
            'page' => $data['page'],
        ]);
        if ($re) {
            return reJson($re, 200, '成功');
        } else {
            return reJson([], 500, '失败');
        }
    }
    
    //热门关键字列表
    public function hotList()
    {
        $data = input('');
        $re = Db::table('qc_key')->where('hot',1)->select();
        if ($re) {
            return reJson($re, 200, '成功');
        } else {
            return reJson([], 500, '失败');
        }
    }
    
    //上传
    public function upload()
    {
        $file = request()->file('file');

        // 上传到本地服务器 /public/storage/resource/20210120/ 目录
        $savename = \think\facade\Filesystem::disk('public')->putFile( 'resource', $file);

        if ($savename) {
            return reJson($savename, 200, '成功');
        } else {
            return reJson([], 500, '失败');
        }
    }
    
    //日志新增、修改
    public function logSave()
    {
        $data = input('');
        $re = Db::table('qc_log')->save($data);
        if ($re) {
            return reJson($re, 200, '成功');
        } else {
            return reJson([], 500, '失败');
        }
    }
    
    //日志列表
    public function logList()
    {
        $data = input('');
        
        $orderKey = isset($data['orderKey']) ? $data['orderKey'] : 'id';
        $orderSort = isset($data['orderSort']) ? $data['orderSort'] : 'desc';
        
        $re = Db::table('qc_log')->order($orderKey, $orderSort)->paginate([
            'list_rows' => 10,
            'page' => $data['page'],
        ]);
        if ($re) {
            return reJson($re, 200, '成功');
        } else {
            return reJson([], 500, '失败');
        }
    }
    
    
}
